import org.w3c.dom.Node;

import java.util.List;
import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86136
 * Date: 2024-09-20
 * Time: 18:48
 */
public class Test {
    //出栈入栈次序匹配
    public static boolean IsPopOrder (int[] pushV, int[] popV) {
        // write code here
        Stack<Integer> stack = new Stack<>();
        int j = 0;
        for(int i = 0; i < pushV.length;i++) {
            stack.push(pushV[i]);
            while(!stack.empty()
                    && j < popV.length
                    && stack.peek() == popV[j]) {
                stack.pop();
                j++;
            }
        }
        return stack.empty();
    }
    //逆波兰表达式求值，中缀表达式转化为后缀表达式
    public static int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for(String str : tokens) {
            if(isNumber(str)) {
                int x = Integer.parseInt(str);
                stack.push(x);
            }else {
                int val2 = stack.pop();
                int val1 = stack.pop();
                switch (str) {
                    case "+":
                        stack.push(val1 + val2);
                        break;
                    case "-":
                        stack.push(val1 - val2);
                        break;
                    case "*":
                        stack.push(val1 * val2);
                        break;
                    case "/":
                        stack.push(val1 / val2);
                        break;
                }
            }
        }
        return stack.pop();
    }
    private  static boolean isNumber(String str) {
        return !(str.equals("+")
                || str.equals("-" )
                || str.equals("*")
                || str.equals("/"));
    }
    //判断字符串是否有效
    public static boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for(int i = 0;i < s.length();i++) {
            char ch = s.charAt(i);
            if(ch == '(' || ch == '[' || ch == '{') {
                stack.push(ch);
            }else {
                if(stack.isEmpty()) {
                    return false;
                }
                //此时开始判断是否匹配
                char ch2 = stack.peek();
                if(ch == ')' && ch2 == '('
                    || ch == ']' && ch2 == '['
                    || ch == '}' && ch2 == '{') {
                    stack.pop();
                }else {
                    return false;
                }
            }
        }
        if(!stack.isEmpty()) {
            return false;
        }
        return true;
    }
    public static void main2(String[] args) {
        //出栈入栈次序匹配
        int[] pushV = {1,2,3,4,5};
        //int[] popV = {4,3,5,1,2};//fasle
        int[] popV = {4,3,5,2,1};//true
        System.out.println(IsPopOrder(pushV, popV));

        //逆波兰表达式求值，中缀表达式转化为后缀表达式
        String[] str = {"10","6","9","3",
                        "+","-11","*","/",
                        "*","17","+","5","+"};
        System.out.println(evalRPN(str));

        //判断字符串是否有效
        String s = "()({[]})";//ture
        //String s = "()({[]}))";//false
        System.out.println(isValid(s));

    }

    public static void main1(String[] args) {
        MyStack stack = new MyStack();
        stack.push(1);
        stack.push(2);
        stack.push(3);

        /*System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());*/

        /*System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.peek());*/

    }

    public static void main(String[] args) {
        MinStack minStack = new MinStack();
        minStack.push(-2);
        minStack.push(-1);
        minStack.push(-2);
        minStack.push(-3);
        minStack.push(1);
        System.out.println(minStack.top());
        System.out.println(minStack.getMin());
        minStack.pop();
        minStack.pop();
        System.out.println(minStack.top());
        System.out.println(minStack.getMin());


    }
}
